﻿Imports System.IO

Public Class frmLawyerPlaintiff

    Dim _PhotoPath As String = ""
    Public _CaseID As Long

    Dim ImageFromLawyer As Byte()

    Private Sub frmLawyerPlaintiff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getComboLawyer()
        reset()
    End Sub

    Protected Sub reset()
        txtLawyerName.Clear()
        If cboGender.Items.Count > 0 Then
            cboGender.SelectedIndex = 0
        Else
            cboGender.SelectedIndex = -1
        End If
        dtpDOB.Value = #1/1/1980#
        txtAddress.Clear()
        txtTel.Clear()
        txtEmail.Clear()
        picPhoto.Image = My.Resources.Photo
        _PhotoPath = ""
        txtLawyerName.Focus()
        chkLawyer.Checked = False
        If cboLawyer.Items.Count > 0 Then
            cboLawyer.SelectedIndex = 0
        Else
            cboLawyer.SelectedIndex = -1
        End If
        ImageFromLawyer = Nothing
    End Sub

    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
        If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _PhotoPath = OpenFileDialog.FileName
            picPhoto.ImageLocation = _PhotoPath
        End If
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        picPhoto.Image = My.Resources.Photo
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtLawyerName.Text) And oc.checknull(cboGender.Text) And oc.checknull(dtpDOB.Value) And oc.checknull(txtAddress.Text) And oc.checknull(txtTel.Text) And oc.checknull(txtEmail.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim oLawyerPlaintiff As New cLawyerPlaintiff
        If checknull() Then
            If oLawyerPlaintiff.insert_LawyerPlaintiff(txtLawyerName.Text.Trim, cboGender.Text, dtpDOB.Value, txtAddress.Text, txtTel.Text.Trim, txtEmail.Text.Trim, getPhoto(), _CaseID, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmCaseEntry.getLawyerPlaintiff()
                reset()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Function getPhoto() As Byte()
        Dim pic As Byte() = Nothing
        If chkLawyer.Checked Then
            pic = ImageFromLawyer
        Else
            If _PhotoPath <> "" Then
                pic = File.ReadAllBytes(_PhotoPath)
            End If
        End If
        Return pic
    End Function

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub

    Protected Sub getComboLawyer()
        Dim oc As New cControl
        oc.getCombo(cboLawyer, "SELECT LawyerID,LawyerName,Gender,DOB,[Address],Tel,Email FROM tblLawyer WHERE Active=1", "LawyerName", "LawyerID")
    End Sub

    Private Sub chkLawyer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkLawyer.CheckedChanged
        If chkLawyer.Checked Then
            getComboLawyer()
            lblLawyer.Enabled = True
            cboLawyer.Enabled = True
        Else
            lblLawyer.Enabled = False
            cboLawyer.Enabled = False
            reset()
        End If
    End Sub

    Protected Sub getTextboxLawyer()
        Dim dt As New DataTable
        Dim oLawyer As New cLawyer
        dt = oLawyer.select_Lawyer_byID(cboLawyer.SelectedValue)
        Try
            txtLawyerName.Text = dt.Rows(0)("LawyerName")
            cboGender.Text = dt.Rows(0)("Gender")
            dtpDOB.Value = dt.Rows(0)("DOB")
            txtAddress.Text = dt.Rows(0)("Address")
            txtTel.Text = dt.Rows(0)("Tel")
            txtEmail.Text = dt.Rows(0)("Email")
            'For Image
            If dt.Rows(0)("Photo") IsNot DBNull.Value Then
                Dim pic As Byte()
                pic = dt.Rows(0)("Photo")
                ImageFromLawyer = dt.Rows(0)("Photo")
                Dim streamPhoto As New MemoryStream(pic, True)
                Dim bmp As Bitmap = New Bitmap(streamPhoto)
                Dim bmp1 As Bitmap = New Bitmap(bmp)
                picPhoto.Image = bmp1
                streamPhoto.Close()
            Else
                picPhoto.Image = My.Resources.Photo
                ImageFromLawyer = Nothing
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cboLawyer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLawyer.SelectedIndexChanged
        If chkLawyer.Checked Then
            getTextboxLawyer()
        End If
    End Sub
End Class